﻿using Newtonsoft.Json;
using RTW.OA.Areas.Common.Servsers;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Common.Controllers
{
    public class NotifyController : Controller
    {
        private NotifyServers notifyServers;

        public NotifyController()
        {
            notifyServers = new NotifyServers();
        }

        // GET: Common/Notify
        public ActionResult Index()
        {

            return View();
        }

        // GET: Common/Notify/Edit/5
        public ActionResult Create(int? id)
        {
            ViewBag.TYPE_IDData = notifyServers.GetTYPE_ID();

            var model = new notify();
            if (id != null)
            {
                ViewBag.Title = "编辑公告表";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建公告表";
            }
            return View(model);
        }

        // GET: Common/Notify/Edit/5
        public ActionResult Details(int? id)
        {
            ViewBag.TYPE_IDData = notifyServers.GetTYPE_ID();

            var model = new notify();
            if (id != null)
            {
                ViewBag.Title = "公告明细";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "公告";
            }
            return View(model);
        }

        // POST: Common/Notify/Edit/5
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(int? id, notify model)
        {
            ViewBag.TYPE_IDData = notifyServers.GetTYPE_ID();

            try
            {
                if (id == null || id == '0')
                {
                    notifyServers.Add(model);
                    ViewBag.msg = "新增公告成功！";
                    model = new notify();

                }
                else
                {
                    notifyServers.Update(model);
                    ViewBag.msg = "修改公告信息成功！";

                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }
        public ActionResult Delete(int id)
        {
            if (id != 0)
            {
                notifyServers.Del(id.ToString());
                ViewBag.msg = "删除公告表信息成功！";
            }
            return Content("");
        }

        // GET: Common/Notify/Delete/5


        #region //GetDate
        public notify GetModel(string id)
        {
            var model = notifyServers.GetModelById(id);
            return model;
        }

        public string GetTableJsonResults(string USER_ID, string BEGIN_DATE, string END_DATE, string TYPE_ID, string SUBJECT)
        {
            var list = notifyServers.GetData(USER_ID, BEGIN_DATE, END_DATE, TYPE_ID, SUBJECT);
            return JsonConvert.SerializeObject(list);
        }
        //public JsonResult GetTableJsonResult()
        //{
        //    var res = new JsonResult();
        //    ////var value = "actionValue"; 
        //    ////db.ContextOptions.ProxyCreationEnabled = false; 
        //    //var list = (from a in db.Articles
        //    //            select new
        //    //            {
        //    //                name = a.ArtTitle,
        //    //                yy = a.ArtPublishTime
        //    //            }).Take(5);
        //    ////记得这里要select new 否则会报错：序列化类型 System.Data.Entity.DynamicProxies XXXXX 的对象时检测到循环引用。 
        //    ////不select new 也行的加上这句 //db.ContextOptions.ProxyCreationEnabled = false; 
        //    //res.Data = list;//返回列表 

        //    //var name = "小华";
        //    //var age = "12";
        //    //var name1 = "小华";
        //    //var age1 = "12";
        //    //res.Data = new object[] { new { name, age }, new { name1, age1 } };//返回一个自定义的object数组 

        //    //var person = new { Name = "小明", Age = 22, Sex = "男" };


        //    res.Data = ""; /*person;*///返回单个对象； 

        //    res.Data = "这是个字符串";//返回一个字符串,意义不大； 

        //    res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取，否则用GET获取是会报错。 
        //    return res;
        //}
        #endregion

    }
}
